package serviceImpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DAO.BaseDAO;
import DTO.Order;
import pageUtil.PageB;
import util.DBConnection;
import util.DBConnection.CallBack;

public class OrderDAO implements BaseDAO<Order> {

	@Override
	public boolean add(Order order, String sql) throws Exception {
		Connection conn = DBConnection.getConn();
        boolean boo = DBConnection.executUpadte(conn, sql,
        		order.getDdsj(),
        		order.getDdcjrid(),
        		order.getCjsj(),
        		order.getZj()
        );
        if(conn!=null){
            conn.close();
        }
        return boo;
	}

	@Override
	public boolean delete(Order t) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(Order t, String sql) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<Order> findAll(int currentPage, int pageSize, String sql) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Order> findAll(String sql) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Order findById(String id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Order findByPropert(Order t) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int dataCount(String sql) throws Exception {
		return DBConnection.queryOne(new CallBack<Integer>() {
            @Override
            public Integer getData(ResultSet rs) {
                int count = 0;
                try {
                    if(rs.next()){
                        count = rs.getInt(1);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs!=null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
                return count;

            }
        }, sql);
	}

	@Override
	public List<Order> findByPage(PageB<Order> bean, String sql) throws SQLException {
		int start = (bean.getCurrentPage()-1)*bean.getPageSize();
        return DBConnection.queryList(new CallBack<Order>() {

            @Override
            public List<Order> getDatas(ResultSet rs) {
                List<Order> list = new  ArrayList<Order>();
                Order order = null;
                try {
                    while(rs.next()){
                    	order = new Order();
                    	order.setId(rs.getInt("id"));
                    	order.setDdsj(rs.getString("ddsj"));
                    	order.setDdcjrid(rs.getString("name"));
                    	order.setCjsj(rs.getString("cjsj"));
                    	order.setZj(rs.getString("zj"));
                        list.add(order);
                    }
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }finally{
                    if(rs!=null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }

                return list;
            }

        }, sql, start,bean.getPageSize());
	}

}
